package com.hitsz.application;

import androidx.appcompat.app.AppCompatActivity;
import com.hitsz.R;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ClockInActivity extends AppCompatActivity implements View.OnClickListener {

    String cat_name = "";
    String haunt_id = "";

    OkHttpClient okHttpClient = new OkHttpClient();
    String TAG = "Cat";
    public String res = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clock_in);

        findViewById(R.id.clock_in_button).setOnClickListener(this);


        // 初始化下拉栏
        Spinner cat_spinner = findViewById(R.id.cat_spinner);
        Spinner haunt_spinner = findViewById(R.id.haunt_spinner);

        // 设置下拉栏适配器
        ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.cats, android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        cat_spinner.setAdapter(adapter1);

        ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.spots, android.R.layout.simple_spinner_item);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        haunt_spinner.setAdapter(adapter2);


        cat_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                cat_name = ClockInActivity.this.getResources().getStringArray(R.array.cats)[i];
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        haunt_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                int id = i + 1;
                haunt_id = id + "";
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.clock_in_button){
            postSend(this.cat_name, this.haunt_id);
            // 睡2秒钟等待响应
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // res处理
            if(res.equals("")){
                Toast toast=Toast.makeText(this, "打卡响应超时！",Toast.LENGTH_SHORT);
                toast.show();
            }else if(res.contains("\"status\":0")){
                Toast toast=Toast.makeText(this, "猫猫打卡成功！",Toast.LENGTH_SHORT);
                toast.show();

                // 页面跳转
                Intent intent = new Intent(ClockInActivity.this, ModeSelectActivity.class);
                startActivity(intent);
                this.finish();
            }
            else{
                Toast toast=Toast.makeText(this, "打卡失败！",Toast.LENGTH_SHORT);
                toast.show();
            }

        }
    }

    public void postSend(String cat_name, String haunt_id){
        new Thread(){
            @Override
            public void run(){
                FormBody formBody = new FormBody.Builder()
                        .add("haunt_id", haunt_id)
                        .add("cat_name",cat_name).build();
                Request request = new Request.Builder()
                        .url("http://192.168.56.1:3007/api/updateHaunt")
                        .post(formBody).build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    res = response.body().string();
                    Log.i(TAG,"body:"+res);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    // 按下返回键回到主菜单
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            Intent intent = new Intent(ClockInActivity.this, ModeSelectActivity.class);
            startActivity(intent);
            // 退出这个界面
            this.finish();
        }
        return true;
    }
}